package UserController

import (
	"encoding/json"
	"fmt"
	"huayan.com/src/goroutinePool"
	"io/ioutil"
	"net/http"
	"time"
)

type UserInfo struct {
	LoginName string `json:"login_name"`
	Pwd       string `json:"pwd"`
}
type ReturnObj struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func LoadUserController() {
	Login()
}

func Login() {
	http.HandleFunc("/user/login", func(writer http.ResponseWriter, request *http.Request) {
		//获取json数据
		body, _ := ioutil.ReadAll(request.Body)
		//json数据反序列化
		var userInfo UserInfo
		json.Unmarshal(body, &userInfo)

		fmt.Println("一个20秒的任务加入进入口池")
		goroutinePool.PoolObj.EntryChannel <- goroutinePool.NewTask(func() error {

			time.Sleep(20 * time.Second)
			return nil
		})

		//设置回应头为json格式
		header := writer.Header()
		header.Set("content-type", "application/json")

		//返回json字符串
		returnObj,_ := json.Marshal(ReturnObj{
			Code: 100,
			Msg:  "成功",
		})
		writer.Write(returnObj)
	})
}
